<?php
/* 
* @Author: LDF QQ 47121862
* @Date:   2014-06-08 12:35:34
* @Last Modified by:   LDF QQ 47121862
* @Last Modified time: 2014-06-12 17:41:16
*/
class CommentControl extends ConfigControl{
	//获取评论ajax--------------?????????
	function index(){
		$this->comment=M('comment')->where(array('aid'=>$art['aid']))->all();
	}

	//文章喜欢+1
	function enjoy(){
		$aid = Q('aid');
		$cl = Q('cl',0,'intval');
		$uid = $_SESSION['user']['uid'];
		//将文章表enjoy+1或-1
		$cl?M('article')->dec('enjoys','aid='.$aid,1):M('article')->inc('enjoys','aid='.$aid,1);
		//判断enjoy表用户是否喜欢决定是插入数据还是删除数据
		// M('enjoy')->add(array('aid'=>$aid,'uid'=>$uid));
		!$cl?M('enjoy')->add(array('aid'=>$aid,'uid'=>$uid)):M('enjoy')->where(array('aid'=>$aid,'uid'=>$uid))->del();
		echo $cl?0:1;die;
	}

	//添加评论ajax
	function add(){
		if(!$_SESSION['user']['uid']) $s=0;
		// 组合评论数据
		$data = $this->formHandle();
		if(M('comment')->add($data)){
			M('article')->inc('comments','aid='.$data['aid'],1); //文章评论数更新
			$s=1;
			$msg='发表评论成功...';
		}else{
			$s=0;
			$msg='发表评论失败...';
		}
		$data['addtime']=date('Y-m-d H:i:s',$data['addtime']);
		$this->lAjax($s,$msg,$data);
	}
	//赞评论
	function zan(){
		$cid=Q('cid',0,'intval');
		$db=M('comment');
		$re=$db->inc('zan','cid='.$cid,1);
		$data=$db->find($cid);
		if($data['zan']>=C('wonderfulComment')) $db->save(array('cid'=>$cid,'wonderful'=>1));
		echo $re;die;
	}
	// 组合评论数据
	private function formHandle(){
		$data = array(
			'content'=>Q('content'),
			'addtime'=>time(),
			'uid'=>$_SESSION['user']['uid'],
			'aid'=>Q('aid',0,'intval')
		);
		return $data;
	}
	//ajax
	private function lAjax($state=null,$msg=null,$data=null){
		echo json_encode(array('state'=>$state,'msg'=>$msg,'data'=>$data));die;
	}
}
